<?php

namespace App\Http\Controllers\Manage;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;
use App\Model\Project;
use App\Model\Client;
use App\Model\Product;
use App\Model\Region;

class ProjectController extends Controller
{

    protected static $field = [
        ['id'=>1,'searchtype'=>'编号'],
        ['id'=>2,'searchtype'=>'创建时间'],
        ['id'=>3,'searchtype'=>'客户名称'],
        ['id'=>4,'searchtype'=>'等级'],
        ['id'=>5,'searchtype'=>'产品'],
        ['id'=>6,'searchtype'=>'预估价格'],
        ['id'=>7,'searchtype'=>'客户报价'],
        ['id'=>8,'searchtype'=>'项目来源'],
        ['id'=>9,'searchtype'=>'阶段'],
        ['id'=>10,'searchtype'=>'来源编号'],
    ];

    private $status = [ // 状态
        '0' => '无效',
        '1' => '有效'
    ];

    private $grade = [ // 等级
        '0' => '一般',
        '1' => '重要',
        '2' => '持续跟进'
    ];

    private $origin = [ // 外部来源
        '0' => '独立',
        '1' => '在线',
        '2' => '转介绍'
    ];

    private $stage = [ // 阶段
        '0' => '初步洽谈',
        '1' => '二次回访',
        '2' => '需求确定',
        '3' => '提案/报价',
        '4' => '签单'
    ];

    private $allow_extension = array('jpg', 'png', 'gif', 'bmp');

    private function get_extension($file) // 获取文件后缀
    {
        return substr(strrchr($file, '.'), 1);
    }

    //
    /**
     * 项目列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function projectList() {
        $user = session('user');
        $search = Input::get('type');
        $param = Input::get('param');
        $orderBy = Input::get('orderBy');
        $page = Input::get('page');
        $count = Input::get('count');
//        dump(input::all());
        $obj = new Project;
        $id = array();
        $dd_id = DB::table("admin")->where("username",$user["name"])->value("dd_id");
        $root = $this->getDeparUser($dd_id);
        foreach ($root as $value){
            $department[] = array('department_id' => $value["department_id"],'department' => $value["department"]);
            foreach ($value["dd_ids"] as $val){
                if($value["is_leader"]){
                    $person[] =  $val;//负责人
                }
            }
        }
        if(!empty($person)){
            $obj = $obj->whereIn("dd_id",$person);
        }else{
            $obj = $obj->where("dd_id",$dd_id);
        }
        if($search == 1){
            $obj = $obj->where('id','like',"%$param%");
        }elseif ($search == 2){
            $obj = $obj->where('create_time','like',"%$param%");
        }elseif ($search == 3){
            $client = Client::where('company','like',"%$param%")->get();
            foreach ($client as $i) {
                $id[] = $i['id'];
            }
            $obj = $obj->whereIn('client_id',$id);
        }elseif ($search == 4){
            if($param == '一般'){
                $p = 0;
            }elseif($param == '重要') {
                $p = 1;
            }
            $obj = $obj->where('grade',$p);
        }elseif ($search == 5){
            $product = Product::where('name','like',"%$param%")->get();
            foreach ($product as $i) {
                $id[] = $i['id'];
            }
            $obj = $obj->whereIn('product_id',$id);
        }elseif ($search == 6){
            $obj = $obj->where('future_price','like',"%$param%");
        }elseif ($search == 7){
            $obj = $obj->where('client_price','like',"%$param%");
        }elseif ($search == 8){
            if($param == '独立'){
                $p = 0;
            }elseif($param == '在线') {
                $p = 1;
            }
            elseif($param == '介绍') {
                $p = 2;
            }
            $obj = $obj->where('origin',$p);
        }elseif ($search == 9){
            if($param == '初步洽谈'){
                $stage = 0;
            }elseif($param == '二次回访') {
                $stage = 1;
            }
            elseif($param == '需求确定') {
                $stage = 2;
            }
            elseif($param == '提案/报价') {
                $stage = 3;
            }
            elseif($param == '签单准备') {
                $stage = 4;
            }
            $obj = $obj->where('stage',$stage);
        }elseif ($search == 10){
            $obj = $obj->where('origin_id','like',"%$param%");
        }elseif ($search == 100){
            $obj = $obj->where('status',$param);
        }
        #判断时间排序
        if($orderBy == 1){
            $obj = $obj->orderBy('update_time','ASC');
        }elseif($orderBy == 2){
            $obj = $obj->orderBy('update_time','DESC');
        }else{
            $obj = $obj->orderBy('rank','DESC')->orderBy('update_time','DESC');
        }
        $project = $obj->Paginate(15);
        if(empty($page)){
            $count = $obj->count();
        }
        if($project == false && !empty($search)){
            return redirect()->back()->withSuccess('未搜索到结果！');
        }
        $searchtype = self::$field;
        return view('manage.project.projectList',[
            'project'=>$project,
            'searchtype'=>$searchtype,
            'search'=>$search,
            'param'=>$param,
            'orderBy'=>$orderBy,
            'count'=>$count,
            'active1' => 3]);
    }

    /**
     * 项目启禁用
     * @return array
     */
    public function projectStatus() {
        $id = Input::get('id');
        $status = Input::get('status');
        if($status == 1) {
            $del = Project::where('id',$id)->update(['status'=>1]);
            $msg = '启用成功！';
        }else{
            $del = Project::where('id',$id)->update(['status'=>0]);
            $msg = '禁用成功！';
        }
        if(!$del){
            return [
                'code'=>101,
                'msg'=>'设置失败！'
            ];
        }
        return [
            'code'=>100,
            'msg'=>$msg
        ];
    }

    public function projectDetail($id) {
        $status = $this->status;
        $grade = $this->grade;
        $origin = $this->origin;
        $stage = $this->stage;
        $project = Project::getProjectInfo($id);

        $file = null; // 文件
        $pic = explode(',', $project->picture); // 图片
        foreach($pic as $key=>$val) {
            if(!in_array($this->get_extension($val), $this->allow_extension)) {
                $file[] = $val;
                unset($pic[$key]);
            }
        }
        return view('manage.project.project_detail',['project'=>$project,'active1' => 3,'status'=>$status, 'grade'=>$grade, 'origin'=>$origin, 'stage'=>$stage,'file'=>$file,'pic'=>$pic]);
    }
}
